package com.bmob.lover.ui.fragment.base

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.bmob.lover.app.ext.getTableName
import com.bmob.lover.app.ext.showDarkEmpty
import com.bmob.lover.app.ext.showDarkError
import com.bmob.lover.app.ext.showDarkLoadingPage
import com.bmob.lover.app.widget.LoadMoreView
import com.bmob.lover.data.model.ObjectBean
import com.bmob.lover.viewmodel.AppViewModel
import com.bmob.lover.viewmodel.EventViewModel
import com.chad.library.adapter.base.loadmore.BaseLoadMoreView
import com.theone.mvvm.core.base.fragment.BasePagerPullRefreshFragment
import com.theone.mvvm.core.base.viewmodel.BaseListViewModel
import com.theone.mvvm.core.databinding.BasePullFreshFragmentBinding
import com.theone.mvvm.core.app.ext.showLoadingPage
import com.theone.mvvm.core.app.widge.TheLoadMoreView
import com.theone.mvvm.core.app.widge.pullrefresh.PullRefreshLayout
import com.theone.mvvm.ext.getAppViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-29 13:55
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
abstract class BasePagerFragment<T : ObjectBean, VM : BaseListViewModel<T>> :
    BasePagerPullRefreshFragment<T, VM, BasePullFreshFragmentBinding>() {

    // 拿到泛型T的类名
    var mTableName: String = this::class.java.getTableName()

    protected val mAppVm: AppViewModel by lazy { getAppViewModel<AppViewModel>() }
    protected val mEventVm: EventViewModel by lazy { getAppViewModel<EventViewModel>() }


    protected open fun isDarkPage(): Boolean = false

    override fun getDataBindingClass(): Class<BasePullFreshFragmentBinding> = BasePullFreshFragmentBinding::class.java

    override fun getAdapterLoadMoreView(): BaseLoadMoreView =
        if (isDarkPage()) LoadMoreView() else TheLoadMoreView()

    override fun getRecyclerView(): RecyclerView = getDataBinding().recyclerView
    override fun getRefreshLayout(): PullRefreshLayout = getDataBinding().refreshLayout

    protected open fun isAutoRefresh(): Boolean = true

    override fun onFirstLoadSuccess(data: List<T>) {
        super.onFirstLoadSuccess(data)
        if (isAutoRefresh()) {
            getContentView().postDelayed({
                onRefresh()
            }, 200)
        }
    }

    override fun onRefreshSuccess(data: List<T>) {
        setRefreshLayoutEnabled(true)
        getAdapter().getDiffer().submitList(data.toMutableList()) {
            getRecyclerView().scrollToPosition(0)
        }
    }

    override fun createObserver() {
        super.createObserver()
        mEventVm.getPublishLiveData().observe(this, Observer {
            if (it == mTableName) {
                onAutoRefresh()
            }
        })
    }

    override fun onFirstLoading() {
        if (isDarkPage()) {
            getLoader()?.showDarkLoadingPage()
        } else {
            showLoadingPage()
        }
        requestServer()
    }

    override fun onFirstLoadError(errorMsg: String?) {
        if (isDarkPage()) {
            getLoader()?.showDarkError(errorMsg)
        } else {
            super.onFirstLoadError(errorMsg)
        }
    }

    override fun onEmptyData() {
        if (isDarkPage()) {
            getLoader()?.showDarkEmpty()
        } else {
            super.onEmptyData()
        }
    }

}